package operation;
import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 洪汤泽大帅哥
 * Date: 2024-11-02
 * Time: 10:56
 */
public class AddBook implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("增加图书");

        if (isFull(bookList)){
            System.out.println("书架满了");
            return;
        }

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您想添加的书籍的名称:");
        String name = sc.nextLine();

        System.out.println("请输入该书籍的作者:");
        String author = sc.nextLine();

        System.out.println("请输入该书籍的价格:");
        int price = sc.nextInt();
        sc.nextLine();

        System.out.println("请输入书籍的类型:");
        String type = sc.nextLine();

        Book book = new Book(name,author,price,type);

        int currSize = bookList.getUsedSize();
        for (int i = 0; i < currSize; i++) {
            Book temp = bookList.getBooks(i);
            if (temp.getName().equals(name)){
                System.out.println("这本书已经存在，不能再添加");
                return;
            }
        }

        //没有这本书，则放在书架中
        bookList.setBooks(currSize,book);

        bookList.setUsedSize(currSize+1);

    }
    private boolean isFull(BookList bookList){
        return bookList.getBooks().length == bookList.getUsedSize();
    }
}
